<template>
  <a-row :gutter="10" style="height: 100%;">
    <a-col :md="14" :sm="24" style="height: 100%;">
      <a-card :bordered="false" style="height: 100%; overflow: auto;">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :sm="12">
              <a-form-item :label="$t('m.system.role.entity.roleName')">
                <a-input placeholder="" v-model="queryParam.roleName$lk"></a-input>
              </a-form-item>
            </a-col>
            <a-col :sm="12">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
                <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="handleAdd" type="primary" icon="plus">{{ $t('m.system.role.other.add_role') }}</a-button>
        <a-button @click="batchDel" type="default" icon="delete" v-test>{{ $t('m.common.button.delete') }}</a-button>
        <a-button type="default" icon="download" @click="handleExportXls($t('m.system.role.other.role_label'))">{{ $t('m.common.button.export') }}</a-button>
        <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                  @change="handleImportExcel">
          <a-button type="default"  icon="import">{{ $t('m.common.button.import') }}</a-button>
        </a-upload>
      </div>

      <!-- table区域-begin -->
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
        :customRow="clickRow"
        :rowClassName="setRowClassName"
        >
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('m.common.button.update') }}</a>
          <a-divider type="vertical"/>
          <a-popconfirm placement="left" :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.id)">
            <a>{{ $t('m.common.button.delete') }}</a>
          </a-popconfirm>
        </template>
      </a-table>
      <!-- table区域-end -->

      <!-- 表单区域 -->
      <role-modal ref="modalForm" @ok="modalFormOk" :back="back"></role-modal>
      <user-role-modal ref="modalUserRole" :back="back"></user-role-modal>
      </a-card>
    </a-col>
    <a-col :md="10" :sm="24" style="height: 100%;">
      <a-card :bordered="false" style="height: 100%; overflow: auto;">
      <RoleUserRight :role="role" :portal="portal"></RoleUserRight>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>

  import RoleModal from './modules/RoleModal'
  import UserRoleModal from './modules/UserRoleModal'
  import { YcListMixin } from '@/mixins/YcListMixin'
  import RoleUserRight from './RoleUserRight'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'RoleList',
    mixins: [YcListMixin],
    components: {
      RoleModal,
      UserRoleModal,
      RoleUserRight
    },
    props: {
      back: String,
      portal: {
        type: String,
        default: '0'
      }
    },
    data() {
      return {
        right: false,
        role: {},
        queryParam: {
          back: this.back
        },
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.system.role.entity.roleName'),
            align: 'center',
            dataIndex: 'roleName',
            width: 160,
          },
          {
            title: this.$t('m.system.role.entity.roleCode'),
            align: 'center',
            dataIndex: 'roleCode',
            width: 100,
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: 'center',
            width: 120,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: YchConstant.PREFIX_SYSTEM + '/role/list',
          delete: YchConstant.PREFIX_SYSTEM + '/role/delete',
          deleteBatch: YchConstant.PREFIX_SYSTEM + '/role/deleteBatch',
          exportXlsUrl: YchConstant.PREFIX_SYSTEM + "/role/exportExcel",
          importExcelUrl: YchConstant.PREFIX_SYSTEM + "/role/importExcel",
        },
        record:{},
      }
    },
    computed: {
      importExcelUrl: function () {
        return `${bootConfig[process.env.NODE_ENV].VUE_APP_API}${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handlePerssion: function(roleId) {
        this.$refs.modalUserRole.show(roleId)
      },
      searchReset() {
        this.queryParam = {
          back: this.back
        }
        this.loadData(1)
      },
      handleOpen(record) {
        this.role = record
        this.right = true
      },
      hideUserList() {
        this.right = false
      },
      afterLoad() {
        this.hideUserList()
      },
      clickRow(record, index){
        return {
          on:{
            click:()=>{
              if(this.record.id !== record.id){
                this.role = record
                this.record = record;
              }
            }
          }
        }
      },
      setRowClassName(record){
        let cls = ''
        if(record.id==this.record.id){
          cls = 'clickRowStyl'
        }
        return cls
      },
    }
  }
</script>
<style lang="less" scoped>

</style>
